package com.qianfeng.smartdevice.service.impl;

import com.qianfeng.smartdevice.cache.SceneCache;
import com.qianfeng.smartdevice.enums.CheckType;
import com.qianfeng.smartdevice.execption.AddDataErrorException;
import com.qianfeng.smartdevice.execption.MyErrorCode;
import com.qianfeng.smartdevice.execption.QueryDataErrorException;
import com.qianfeng.smartdevice.mapper.SysSceneMapper;
import com.qianfeng.smartdevice.pojo.SysScene;
import com.qianfeng.smartdevice.pojo.SysUserInfo;
import com.qianfeng.smartdevice.service.SysSceneService;
import com.qianfeng.smartdevice.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;

/**
 * Created by wenka on 2022/3/25 18:57
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
@Service
@Transactional
public class SysSceneServiceImpl implements SysSceneService {
    private SysSceneMapper sysSceneMapper;
    private SceneCache sceneCache;

    @Autowired
    public void setSceneCache(SceneCache sceneCache) {
        this.sceneCache = sceneCache;
    }

    @Autowired
    public void setSysSceneMapper(SysSceneMapper sysSceneMapper) {
        this.sysSceneMapper = sysSceneMapper;
    }

    @Override
    public void addSysScene(SysScene sysScene) {
        //传递数据不能为空
        Assert.isTrue(!sysScene.isEmpty(CheckType.ADD), () -> {
            throw new AddDataErrorException("传递的数据不能为空", MyErrorCode.DATA_NULL);
        });
        //获取登录用户
        SysUserInfo loginUser = SecurityUtils.getLoginUser();
        //我们要求必须登录
        Assert.notNull(loginUser, () -> {
            throw new AddDataErrorException("未登录", MyErrorCode.NOT_LOGIN);
        });
        //我们要求一个用户的场景名称不能重复
        SysScene byUserAndName = sysSceneMapper.findByUserAndName(loginUser.getUId(), sysScene.getSceneName());
        //根据用户和场景的名字查询，如果能查询到，则代表村子（检查是不是标记为删除），查询不到，就不存在
        //如果数据已存在，但是为删除状态，我们就更新，如果不存在，我们就添加
        Assert.isTrue(byUserAndName == null || byUserAndName.getStatus() == 0, () -> {
            throw new AddDataErrorException("场景已存在", MyErrorCode.DATA_ALLREADY_EXIST);
        });
        //如果为空，证明不存在就添加，否则就更新
        if (byUserAndName == null) {
            sysScene.setCreateBy(loginUser.getUId());
            sysScene.setCreateTime(new Date());
            sysSceneMapper.addSysScene(sysScene);
        } else {
            //更新
            sysSceneMapper.updateStatus(sysScene.getSceneId(), 1L);
        }


    }

    @Override
    public SysScene findByUserAndName(Long userId, String scenceName) {
        return null;
    }

    @Override
    public int updateStatus(Long sceneId, Long status) {
        return 0;
    }

    @Override
    public SysScene findById(Long id) {
        SysUserInfo userInfo = SecurityUtils.getLoginUser();
        Assert.notNull(userInfo, () -> {
            throw new AddDataErrorException("未登陆", MyErrorCode.NOT_LOGIN);
        });
        //查询数据的时候需要根据id和当前的用户进行查询
        //方式1 查询数据库的条件就是主键和用户的id

        //方式2  根据主键查询数据, 然后代码中判断用户
        //从缓存中查询
        SysScene sysScene = sceneCache.get(id);
//        Assert.notNull(sysScene.getSceneId(),()->{
//            throw new QueryDataErrorException("数据不存在", MyErrorCode.DATA_NULL);
//        });

//        Assert.isTrue(sysScene.getSceneId()!=null,()->{
//            throw new QueryDataErrorException("数据不存在", MyErrorCode.DATA_NULL);
//        });
//        //判断数据是不是属于当前用户的
//        Assert.isTrue(sysScene.getCreateBy()==userInfo.getUId(),()->{
//            throw new QueryDataErrorException("数据不存在", MyErrorCode.DATA_NULL);
//        });

        Assert.isTrue(sysScene.getSceneId() != null && sysScene.getCreateBy().equals(userInfo.getUId()), () -> {
            throw new QueryDataErrorException("数据不存在", MyErrorCode.DATA_NULL);
        });
        return sysScene;
    }
}
